package com.example.vv.model.home.base.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.vv.R;
import com.example.vv.adapter.BaseRecyclerViewAdapter;
import com.example.vv.fragmentWork.entity.ArticleEntity;
import com.example.vv.fragmentWork.entity.TopEntity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class TopRecycleViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements View.OnClickListener {
    Context context;
    List<TopEntity> list;
    List<ArticleEntity.DatasBean> list2 = new ArrayList<>();
    private OnItemClickListener listener;
    int layid;

    public TopRecycleViewAdapter(Context context, List<TopEntity> list, int layid) {
        this.context = context;
        this.list = list;
        this.layid = layid;
    }

    View inflate;

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        inflate = LayoutInflater.from(context).inflate(R.layout.item_home_list, parent, false);
        inflate.setOnClickListener(this);
        ViewHolder holder = new ViewHolder(inflate);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        holder.itemView.setTag(position);
        TopEntity topEntity;
        ArticleEntity.DatasBean articleEntity;
        if (position < list.size()) {
            topEntity = list.get(position);
            ((ViewHolder) holder).homeContent.setText(topEntity.getTitle() + "");
            ((ViewHolder) holder).home_type_bd.setText("置顶");
            ((ViewHolder) holder).homeTime.setText(topEntity.getNiceDate() + "");
            ((ViewHolder) holder).homeAuthor.setText(topEntity.getAuthor().equals("") ? topEntity.getShareUser() : topEntity.getAuthor() + "");
            ((ViewHolder) holder).homeBottomType.setText(topEntity.getSuperChapterName() + "");
            ((ViewHolder) holder).homeBottomType1.setText(topEntity.getChapterName() + "");
            ((ViewHolder) holder).home_type_fl.setText(topEntity.getSuperChapterName() + "");

        } else {
            int i = position - list.size();
            articleEntity = list2.get(i);
            ((ViewHolder) holder).homeContent.setText(articleEntity.getTitle() + "");
            if (articleEntity.getNiceDate().equals("1天前") || articleEntity.getNiceDate().equals("刚刚")) {
                ((ViewHolder) holder).home_type_bd.setText("新");
            } else {
                ((ViewHolder) holder).home_type_bd.setVisibility(View.GONE);
            }
            ((ViewHolder) holder).homeTime.setText(articleEntity.getNiceDate() + "");
            ((ViewHolder) holder).homeAuthor.setText(articleEntity.getAuthor().equals("") ? articleEntity.getShareUser() : articleEntity.getAuthor() + "");
            ((ViewHolder) holder).homeBottomType.setText(articleEntity.getSuperChapterName() + "");
            ((ViewHolder) holder).homeBottomType1.setText(articleEntity.getChapterName() + "");
            ((ViewHolder) holder).home_type_fl.setText(articleEntity.getSuperChapterName() + "");

        }
    }

    public void setOnItemClickListener(OnItemClickListener clickListener) {
        this.listener = clickListener;
    }

    @Override
    public void onClick(View view) {
        if (listener != null) {
            listener.OnItemClickListener(this, view, (Integer) view.getTag());
        }
    }

    public interface OnItemClickListener {
        void OnItemClickListener(RecyclerView.Adapter adapter, View v, int position);
    }

    @Override
    public int getItemCount() {
        return list2.size() == 0 ? list.size() : list.size() + list2.size();
    }

    public void setList(List<ArticleEntity.DatasBean> list11) {
        list2.addAll(0,list11);
//        for (int i = 0; i < list11.size(); i++) {
//            list2.add(list11.get(i));
//        }
        Log.i("TAG", "setList: " + "list2:" + list11.size() + "  --" + "list: " + list.size() + "  --" + "list2: " + list2.size() + "---" + getItemCount());
        notifyDataSetChanged();
    }


    static
    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.home_type)
        TextView homeType;
        @BindView(R.id.home_biao)
        TextView homeBiao;
        @BindView(R.id.home_author)
        TextView homeAuthor;
        @BindView(R.id.home_time)
        TextView homeTime;
        @BindView(R.id.home_content)
        TextView homeContent;
        @BindView(R.id.home_bottom_type)
        TextView homeBottomType;
        @BindView(R.id.home_bottom_type1)
        TextView homeBottomType1;
        @BindView(R.id.home_type_bd)
        TextView home_type_bd;
        @BindView(R.id.home_type_fl)
        TextView home_type_fl;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
